<?php

namespace App\Service;

use App\Model\DevicePowerLog;
use App\Model\DeviceWarning;
use App\Utils\Logger;
use App\Model\Device;
use App\Model\User;
use Psr\Container\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;

class DevicePowerLogService
{
    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;


    protected Redis $redis;


    public function __construct()
    {
        $this->redis = ApplicationContext::getContainer()->get(Redis::class);
    }

    public function update($deviceId, $du, $total, $power, $xh, $volt = 0, $child_volt = 0, $curr = 0, $child_curr = 0, $signal=0)
    {
        $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
        $key = 'power-log-' . $deviceId;
        $last = DevicePowerLog::query()->where('device_id', $deviceId)
            ->orderBy('id', 'desc')
            ->value('total');
        if ($last >= $total) {
            $xh = 0;
        } else {
            $xh = $total - $last;
        }
        if ($cache->get($key)) {
            return;
        }
        if ($volt > 0) {
            $volt = $volt / 1000;
        }
        if ($curr > 0) {
            $curr = $curr / 1000;
        }

        if ($child_volt > 0) {
            $child_volt = $child_volt / 1000;
        }
        if ($child_curr > 0) {
            $child_curr = $child_curr / 1000;
        }
        if($power == 0 && $du == 0 && $total == 0){
            return '';
        }
        DevicePowerLog::create([
            'device_id' => $deviceId,
            'du' => $du,
            'total' => $total,
            'power' => $power,
            'xh' => $xh,
            'volt' => $volt,
            'child_volt' => $child_volt,
            'curr' => $curr,
            'child_curr' => $child_curr,
            'is_sen' => 1,
            'signal' => $signal
        ]);
        $cache->set($key, 1, 300);
        $time = time();
        $hourKey = 'zhufang_database_' . $deviceId . '-' . date('YmdH', $time);
        $dayKey = 'zhufang_database_' . $deviceId . '-' . date('Ymd', $time);
        $monthKey = 'zhufang_database_' . $deviceId . '-' . date('Ym', $time);
        
        
          $this->redis->select(2);
        
         if(!$this->redis->exists('sub_warning_'.$deviceId)){
            $device = Device::where('id',$deviceId)->first();
            if($device->mobile){
               $user = User::where('mobile',$device->mobile)->first();
               if($user && $user->openid && $user->sub_2 && $du < $user->sub_warning*1000 ){
                   $warning['device_id'] = $deviceId;
                   $warning['du'] = $du;
                   $warning['user_id'] = $user->id;
                   $warning['openid'] = $user->openid;
                   $warning['sub_warning'] = $user->sub_warning;
                   $warning['sn'] = $device->sn;
                   $warning['house_id'] = $device->house_id;
                   $this->redis->rPush('zhufang_database_queue:sub_warning',json_encode($warning));
                //   $result = $this->redis->lLen('zhufang_database_queue:sub_warning');
                //   $device->name = $result;
                //   $device->save();
                   $this->redis->setex('sub_warning_'.$deviceId, 3600 * 24, 1);
               }
            } 
         }
        
        
        
        
        
        
        if ($xh == $total) {
            return '';
        }

        if ($this->redis->exists($hourKey)) {
            if ($xh > 0) {
                $this->redis->incrBy($hourKey, $xh);
            }
        } else {
            if ($xh > 0) {
                $this->redis->setex($hourKey, 3600 * 24 * 90, $xh);
            }
        }
        if ($this->redis->exists($dayKey)) {
            if ($xh > 0) {
                $this->redis->incrBy($dayKey, $xh);
            }
        } else {
            if ($xh > 0) {
                $this->redis->setex($dayKey, 3600 * 24 * 730, $xh);
            }
        }
        if ($this->redis->exists($monthKey)) {
            if ($xh > 0) {
                $this->redis->incrBy($monthKey, $xh);
            }
        } else {
            if ($xh > 0) {
                $this->redis->setex($monthKey, 3600 * 24 * 730, $xh);
            }
        }

        // try {
        //     if ($xh > 5) {
        //         $device = Device::query()->where('id', $deviceId)->first();
        //         if ($device && $device->is_warning == 1) {
        //             $device->is_warning = 0;
        //             $device->save();
        //             $deviceWarning = DeviceWarning::query()->where('device_id', $deviceId)->orderBy('id', 'desc')
        //                 ->first();
        //             if($deviceWarning){
        //               $deviceWarning->re_time = date('Y-m-d H:i:s');
        //                 $deviceWarning->re_xh = $xh;
        //                 $deviceWarning->save();           
        //             }    
             
        //         }
        //     }

        // } catch (\Exception $e) {
        //     Logger::get('mqtt')->notice('心跳电量异常报错'.$e->getMessage());
        // }
    }
}
